<?php 

class Get{
	
	var $db;
	var $cache;
	var $dbpre;
	
	
	public function Get($db,$cache,$dbpre){
		$this->__construct();
	}
	
	public function __construct($db,$cache,$dbpre){
		$this->db = $db;
		$this->cache = $cache;
		$this->dbpre = $dbpre;
	}
	
   
	
	/**
	 * 获取竞拍数据
	 * 竞拍商品
	 * $key商品竞拍ID
	 */
	//如果没有缓存产生缓存(获取竞拍缓存);
	public function getAuctionData($key){
		global $sysconfig;
		
		
		$data = $this->cache->get($key);
		$time = time();
		if (empty($data)) {
			$sql = "SELECT A.*,U.head FROM `{$this->dbpre}auction` as A left join `{$this->dbpre}user` as U on A.uid = U.id where A.id={$key}";
			$data = $this->db->fetch_first($sql);  //获取竞标商品最后竞标人
			if(!empty($data)){
				$data['uptime']=microtime(true);
				$this->cache->set($key,$data);
			}else{
				return false;
			}
		}
		
		
		
		
		//TODO:....................................机器人自动竞标  start
		
		//TODO:................机器人结束条件判断有问题
		
		//判断是否机器人 如果符合条件进行机器人点击
		// if(1) 为true   if(0) 为false
		if ($data['robots']){   //.....是否开启机器人：1开启，0未开启
			
			//...注册用户投标金额达到目标金额后机器人就不会继续投标
			if ($data['realprice'] - $data['robotspromote'] < $data['target']){    //商品竞标金额 - 机器人竞标金额  < 机器人设置目标金额  （实际就是非机器人竞标金额）
				
				
				//TODO:需要考虑注册用户竞标可通过虚拟币和实体币竞标，虚拟币只能作为竞标用，但是不能核算成本
				//TODO:此判断条件是否可通过商品竞标历史中注册用户实体竞标币折算金额同成本价之间进行控制
				
				
				$surplus = $data['endtime'] - $time;    //结束时间和当前时间间隔
				if ($surplus < $data['max']){   //机器人设置触发最大值
					if ($surplus <= $data['min']){  //机器人设置触发最小值
						$info = $this->_robot($data);  //....机器人点击
					}else {
						$bl = lucker($sysconfig['robots_probability']);//触发概率
						if ($bl){
							$info = $this->_robot($data);
						}						
					}
				}
			}
		}

		
		//TODO:....................................机器人自动竞标  end
		
		
		
		if(!empty($info)){
			$data = $info;
		}
		
		
		
		return $data;
	}
	
	
	
	
	/**
	 * 
	 * 查询竞拍历史（最近10条）
	 * 查看商品竞拍历史
	 * $id：商品竞拍ID
	 */
	//如果没有缓存产生缓存(获取竞拍缓存);
	public function getLogData($id){
		$key = 'history_'.$id;
		$data = $this->cache->get($key);
		if (empty($data)) {
			$sql = "SELECT * FROM `{$this->dbpre}auction_log` where aid={$id} order by id desc limit 10";
			$query = $this->db->query($sql);
			while($value = $this->db->fetch_array($query)) {
				$data[]=$value;
			}
			$this->cache->set($key,$data);
		}
		return $data;
	}
	
	
	
	
	/**
	 * 查询商品竞拍机器人设置
	 * $id:商品竞拍ID
	 */
	//如果没有缓存产生缓存(获取竞拍缓存);
	public function getUserData($id){
		$key = 'user_'.$id;
		$data = $this->cache->get($key);
		if (empty($data)) {
			$sql = "SELECT U.* FROM `{$this->dbpre}robot` as R left join `{$this->dbpre}user` as U on R.uid = U.id where R.aid={$id}";
			$query =$this->db->query($sql);
			while($value = $this->db->fetch_array($query)) {
				$data[]=$value;
			}
			$this->cache->set($key,$data);
		}
		return $data;
	}
	
	
	
	
	
	/**
	 * 机器人点击
	 * Enter description here ...
	 * @param unknown_type $auctiondata
	 */
	public function _robot($auctiondata){
		$user = $this->getUserData($auctiondata['id']);   //竞标竞标ID

		if(count($user) >= 3){   //商品竞拍机器人个数
			do {
				$i = array_rand($user);
				$userdata = $user[$i];
			}while ($userdata['id'] == $auctiondata['uid']); //最后竞拍人
		}else{
			return false;
		}
		
		
		//竞拍参与表
		$sql = "SELECT * FROM `{$this->dbpre}participate` where `aid`={$auctiondata['id']} and `uid`={$userdata['id']}";
		$participate = $this->db->fetch_first($sql);
		unset($sql);
		if (empty($participate)){   //如果商品竞标参与表中不存在则记录进去
			$sql = "INSERT INTO `{$this->dbpre}participate` (`id`, `uid`, `aid`) VALUES (NULL, '{$userdata['id']}', '{$auctiondata['id']}')";
			$this->db->query($sql);
			unset($sql);
		}

		
		$currentprice = $auctiondata['realprice'] + $auctiondata['addprice'];     //当前价格 = 当前价格 + 竞标金额增加值
		$robotspromote = $auctiondata['robotspromote'] + $auctiondata['addprice'];  //机器人价格 = 机器人金额 + 竞标金额增加值

		$ip = $userdata['ip'];     //IP地址
		$address = getlocaltion($ip);   //根据IP获取地址
		$time = time();
		
		//...插入竞标历史表
		$sql = "INSERT INTO `{$this->dbpre}auction_log` (`id`, `aid`, `uid`, `name`, `goodsname`, `beat`, `pointb`, `ip`, `address`, `bidprice`, `addtime`) 
				VALUES (NULL, '{$auctiondata['id']}', '{$userdata['id']}', '{$userdata['name']}', '{$auctiondata['goodsname']}', '0'
				,'{$auctiondata['usebeat']}', '{$ip}', '{$address}', '{$currentprice}', '{$time}')";
		
		
		$logdata = $this->getLogData($auctiondata['id']); //先获取缓存
		
		$this->db->query($sql); //再写进数据库
		
		$log_id = $this->db->insert_id(); //获取刚才写入的ID
		
		unset($sql);
		$sql = "SELECT * FROM `{$this->dbpre}auction_log` WHERE `id` = {$log_id}";
		$tmp = $this->db->fetch_first($sql);//再把它读出来
		
		$num = array_unshift($logdata,$tmp);     //然后对 缓存内容进行更新
		if ($num > 10){
			array_pop($logdata);
		}
		$key = 'history_'.$auctiondata['id'];
		$this->cache->set($key,$logdata);
		
		
		
		//TODO:
		//对竞拍内容的更新
		if($auctiondata['maxaddsecond']>0){
			$diftime = $auctiondata['endtime']-$time;
			if($diftime < $auctiondata['maxaddsecond']){
				$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
				if($currenttime-$time > $auctiondata['maxaddsecond'])
					$currenttime=$time+$auctiondata['maxaddsecond'];
			}else{
				$currenttime = $auctiondata['endtime'];
			}
		}else{
			$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
			if ($currenttime < time())
				$currenttime = time() + $auctiondata['addsecond'];
		}
		
		
		
		//对数据库的竞拍数据更新
		$auctioncount = $auctiondata['auctioncount']+1;     //竞拍次数
		unset($sql);
		$sql = "UPDATE `{$this->dbpre}auction` SET `uid` = '{$userdata['id']}', `auctionuname` = '{$userdata['name']}', 
				`auctioncount` = '{$auctioncount}', `realprice` = '{$currentprice}', `robotspromote` = '{$robotspromote}', `endtime` = '{$currenttime}' 
				WHERE `id` = {$auctiondata['id']}";
		$this->db->query($sql); //写进数据库
		
		
		//更新缓存中的内容
		$data = array(
			'uid'=>$userdata['id'],
			'auctionuname'=>$userdata['name'],
			'auctioncount'=>$auctioncount,
			'realprice'=>$currentprice,
			'robotspromote'=>$robotspromote,
			'endtime'=>$currenttime,
			'head'=>$userdata['head'],
			'uptime'=>microtime(true)
		);
		
		$cachedata = array_merge($auctiondata,$data);
		
		$this->cache->set($auctiondata['id'],$cachedata);
		
		return $cachedata;
	}
	
	
	
	
	
}
?>